﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.WebApp;
using Gift.Model;

public partial class admin_manager_department : BaseAdmin
{
  
    tbDepartment oDepartment;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                this.ValidateLoginRule(Gift.Model.ENUM.ERule.DepartmentEdit);
            }
            else
            {
                this.ValidateLoginRule(Gift.Model.ENUM.ERule.DepartmentAdd);
            }
            this.BuildForm();
        }
    }

    void BuildForm()
    {
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            oDepartment = BaseFactory.theDepartment.GetModel(int.Parse(Request.QueryString["id"]));
            this.txtDepName.Value = oDepartment.DepName;
            this.txtDepCode.Value = oDepartment.DepCode;
            this.txtDepTel.Value = oDepartment.DepTel;
            this.txtDepManager.Value = oDepartment.DepManager;
            this.txtDeMobile.Value = oDepartment.ManagerMobile;
        }
 
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            oDepartment = BaseFactory.theDepartment.GetModel(int.Parse(Request.QueryString["id"]));
            oDepartment.DepName = this.txtDepName.Value.Trim();
            oDepartment.DepCode = this.txtDepCode.Value.Trim();
            oDepartment.DepTel = this.txtDepTel.Value.Trim();
            oDepartment.DepManager = this.txtDepManager.Value.Trim();
            oDepartment.ManagerMobile = this.txtDeMobile.Value.Trim();
            BaseFactory.theDepartment.Update(oDepartment);
        }
        else
        {
            oDepartment = new tbDepartment();
            oDepartment.DepName = this.txtDepName.Value.Trim();
            oDepartment.DepCode = this.txtDepCode.Value.Trim();
            oDepartment.DepTel = this.txtDepTel.Value.Trim();
            oDepartment.DepManager = this.txtDepManager.Value.Trim();
            oDepartment.ManagerMobile = this.txtDeMobile.Value.Trim();
            oDepartment.CompanyId = int.Parse(base.Administrator.CompanyId);
            BaseFactory.theDepartment.Add(oDepartment);
        }

        base.Response.Write("<script language='javascript'>;alert('保存成功！');window.location.href='DepartmentList.aspx';</script>");
    }
}